package hbase.DelFriends;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class HbaseClientForMyFriends {
    Configuration conf = null;
    Connection conn = null;

    @Before
    public void init() throws IOException {
        //获取一个配置文件对象
        conf = HBaseConfiguration.create();

        conf.set("hbase.zookeeper.quorum", "linux121,linux122");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        //通过conf获取到hbase集群的连接
        conn = ConnectionFactory.createConnection(conf);
    }

    //创建一张hbase表
    @Test
    public void createTable() throws IOException {
        //获取HbaseAdmin对象用来创建表
        HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
        //创建Htabledesc描述器，表描述器
        final HTableDescriptor lagou_friends = new HTableDescriptor(TableName.valueOf("lagou_friends"));
        //指定列族
        lagou_friends.addFamily(new HColumnDescriptor("friends"));
        admin.createTable(lagou_friends);
        System.out.println("lagou_friends表创建成功！！");
    }

    /**
     * 插入朋友数据
     *   uid_001  uid_002 uid_003 uid_004 uid_005
     *   uid_002  uid_001 uid_003 uid_004
     *   uid_003  uid_002
     *   uid_004
     *   uid_005
     * */
    @Test
    public void putData() throws IOException {
        //需要获取一个table对象
        final Table lagou_friends = conn.getTable(TableName.valueOf("lagou_friends"));

        final Put put = new Put(Bytes.toBytes("uid_001"));//指定rowkey

        put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid_002"), Bytes.toBytes("uid_002"));
        put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid_003"), Bytes.toBytes("uid_003"));
        put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid_004"), Bytes.toBytes("uid_004"));
        put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid_005"), Bytes.toBytes("uid_005"));

        final Put put1 = new Put(Bytes.toBytes("uid_002"));//指定rowkey

        put1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid_001"), Bytes.toBytes("uid_001"));
        put1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid_003"), Bytes.toBytes("uid_003"));
        put1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid_004"), Bytes.toBytes("uid_004"));


        final Put put2 = new Put(Bytes.toBytes("uid_003"));//指定rowkey

        put2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid_002"), Bytes.toBytes("uid_002"));

        //插入数据，参数类型是put
        lagou_friends.put(put);
        lagou_friends.put(put1);
        lagou_friends.put(put2);

        //准备list<puts>,可以执行批量插入
        //关闭table对象
        lagou_friends.close();
        System.out.println("插入数据到lagou_friends表成功！！");
    }

    //删除一条好友信息
    @Test
    public void deleteData() throws IOException {
        //需要获取一个table对象
        final Table lagou_friends = conn.getTable(TableName.valueOf("lagou_friends"));

        //准备delete对象
        final Delete delete = new Delete(Bytes.toBytes("uid_003"));
        delete.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid_002"));
        //执行删除
        lagou_friends.delete(delete);
        //关闭table对象
        lagou_friends.close();
        System.out.println("删除数据成功！！");
    }


    /**
     * 插入朋友数据
     *   uid_001  uid_002 uid_003 uid_004 uid_005
     *   uid_002  uid_001 uid_003 uid_004
     *   uid_003  uid_002
     *   uid_004
     *   uid_005
     * */
    @Test
    public void putData_toff() throws IOException {
        //需要获取一个table对象
        final Table lagou_friends = conn.getTable(TableName.valueOf("lagou_friends"));

        final Put put2 = new Put(Bytes.toBytes("uid_001"));//指定rowkey

        put2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid_002"), Bytes.toBytes("uid_002"));

        //插入数据，参数类型是put
        lagou_friends.put(put2);

        //准备list<puts>,可以执行批量插入
        //关闭table对象
        lagou_friends.close();
        System.out.println("插入数据到lagou_friends表成功！！");
    }


    //删除一条好友信息
    @Test
    public void deleteData_toff() throws IOException {
        //需要获取一个table对象
        final Table lagou_friends = conn.getTable(TableName.valueOf("lagou_friends"));

        //准备delete对象
        final Delete delete = new Delete(Bytes.toBytes("uid_005"));
        delete.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid_002"));
        //执行删除
        lagou_friends.delete(delete);
        //关闭table对象
        lagou_friends.close();
        System.out.println("删除数据成功！！");
    }
    //释放连接
    @After
    public void realse() {
        if (conn != null) {
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
